package net.asfun.jvalog.service;

import net.asfun.jvalog.domain.Dbcache;
import net.asfun.jvalog.domain.Gmcache;
import net.asfun.jvalog.misc.ThreadHelper;

public class DualCacher {
	
	private DualCacher(){}
	
	private static String get(String key) {
//		if(true) return null;
		Object obj = Gmcache.get(key);
		if ( null != obj ) {
			return obj.toString();
		} else {
			String value = Dbcache.get(key);
			if ( value != null ) {
				Gmcache.put(key, value);
			}
			return value;
		}
	}
	
	private static void put(String key, String value) {
		Gmcache.put(key, value);
		Dbcache.put(key, value);
	}

	public static String rss() {
		String key = "rss";
		String value = get(key);
		if ( value == null ) {
			value = Painter.rss();
			put(key, value);
		}
		ThreadHelper.header().setMime("application/rss+xml");//text/xml
		return value;
	}

	public static String atom() {
		String key = "atom";
		String value = get(key);
		if ( value == null ) {
			value = Painter.atom();
			put(key, value);
		}
		ThreadHelper.header().setMime("application/atom+xml");//text/xml
		return value;
	}

	public static String sitemap() {
		String key = "sitemap";
		String value = get(key);
		if ( value == null ) {
			value = Painter.sitemap();
			put(key, value);
		}
		ThreadHelper.header().setMime("text/xml");
		return value;
	}
	
	public static String tag() {
		String key = "tag";
		String value = get(key);
		if ( value == null ) {
			value = Painter.tag();
			put(key, value);
		}
		return value;
	}

	public static String tag(String name, int page) {
		String key, value;
		key = "tag/" + name + "/" + page;
		value = get(key);
		if ( value == null ) {
			value = Painter.tag(name, page);
			put(key, value);
		}
		return value;
	}
	
	public static String category() {
		String key, value;
		key = "category";
		value = get(key);
		if ( value == null ) {
			value = Painter.category();
			put(key, value);
		}
		return value;
	}

	public static String category(String name, int page) {
		String key, value;
		key = "category/" + name + "/" + page;
		value = get(key);
		if ( value == null ) {
			value = Painter.category(name, page);
			put(key, value);
		}
		return value;
	}

	public static String main(int page) {
		String key = "post/" + page;
		String value = get(key);
		if ( value == null ) {
			value = Painter.index(page);
			put(key, value);
		}
		return value;
	}

	public static String media(String name) {
		if ( null == name || "".equals(name.trim()) ) {
			return notFound();
		}
		String key = "media/" + name;
		String value = get(key);
		if ( value == null ) {
			value = Painter.media(name);
			put(key, value);
		}
		return value;
	}
	
	public static String date(String[] ymd, int page) {
		String key = null;
		switch (ymd.length) {
		case 1 :
			key = ymd[0] + "/?p=" + page;
			break;
		case 2 :
			key = ymd[0] + "/" + ymd[1] + "/?p=" + page;
			break;
		case 3 :
			key = ymd[0] + "/" + ymd[1] + "/" + ymd[2] + "/?p=" + page; 
		}
		String value = get(key);
		if ( value == null ) {
			value = Painter.date(ymd, page);
			put(key, value);
		}
		return value;
	}

	public static String post(String slogan, String day, String month,
			String year) {
		if ( null == year || "".equals(year.trim()) || 
			 null == month || "".equals(month.trim()) ||
			 null == day || "".equals(day.trim()) ||
			 null == slogan || "".equals(slogan.trim()) ) {
			return notFound();
		}
		String key = year + "/" + month + "/" + day + "/" + slogan;
		String value = get(key);
		if ( null == value ) {
			value = Painter.post(key);
			put(key, value);
		}
		return value;
	}
	
	public static String notFound() {
		String key = "404";
		String value = get(key);
		if ( value == null ) {
			value = Painter.error(key);
			put(key, value);
		}
		return value;
	}

	public static String error() {
		String key = "500";
		String value = get(key);
		if ( value == null ) {
			value = Painter.error(key);
			put(key, value);
		}
		return value;
	}

	public static String page(String slogan) {
		String key = "page/" + slogan;
		String value = get(key);
		if ( value == null ) {
			value = Painter.page(key);
			put(key, value);
		}
		return value;
	}

}
